﻿@{
    ViewBag.Title = "菜单管理";
    Layout = "~/Views/Shared/_Index.cshtml";
}
@using Raffles.OA.Utility
@using Raffles.OA.Data
<link href="~/Content/plugin/bootstrap.treetable/bootstrap-treetable.min.css" rel="stylesheet" />
<script src="~/Content/plugin/bootstrap.treetable/bootstrap-treetable.min.js"></script>
<script src="~/Content/framework/js/framework-bootstrap-treetable-plugin.js"></script>
<style>
    table th, td {
        text-align: center !important;
    }
</style>
<section class="content">
    <div class="row">
        <div class="col-xs-12">
            <div class="box box-info">
                <div class="box-body">
                    <div class="table-responsive">
                        <div class="btn-group-sm" id="toolbar" role="group">
                            <a id="btnAdd" authorize="yes" class="btn btn-success" onclick="showForm(true)"><i class="fa fa-plus"></i> 新增</a>
                            <a id="btnEdit" authorize="yes" class="btn btn-primary" onclick="showForm(false)"><i class="fa fa-edit"></i> 修改</a>
                            <a id="btnDelete" authorize="yes" class="btn btn-danger" onclick="deleleNode()"><i class="fa fa-remove"></i> 删除</a>
                            <a id="btnExpandAll" class="btn btn-info"><i class="fa fa-exchange"></i> 展开/折叠</a>
                        </div>
                        <table id="gridTable" class="table table-hover table-striped text-nowrap">
                        </table>
                    </div>
                </div>
            </div>
        </div>
    </div>
</section>
<script type="text/javascript">
    var parentId = "";
    var id = "";
    $(function () {
        initTreeGrid();
        // bootstrap table tree 表格树 展开/折叠
        var expandFlag = true;
        $("#btnExpandAll").click(function () {
            if (expandFlag) {
                $('#gridTable').bootstrapTreeTable('expandAll');
            } else {
                $('#gridTable').bootstrapTreeTable('collapseAll');
            }
            expandFlag = expandFlag ? false : true;
        });
    });
    function initTreeGrid() {
        var options = {
            toolbar: '#toolbar',
            expandAll: false,
            expandFirst: false,
            url: '@Url.Content("/Manage/Menu/GetTreeData")',
            columns: [
                { field: 'selectItem', radio: true},
                {
                    field: 'Name', title: '菜单名称', align: "center", valign: 'middle',
                    formatter: function (value, row, index) {
                        if (frame.isNullOrEmpty(row.Icon)) {
                            return row.Name;
                        } else {
                            return '<i class="' + row.Icon + '"></i> <span class="nav-label">' + row.Name + '</span>';
                        }
                    }
                },
                {
                    field: 'Type', title: '类型', align: "center", valign: 'middle',
                    formatter: function (value, item, index) {
                        if (value == "@((int)MenuTypeEnum.Directory)") {
                            return '<span class="label label-success">@MenuTypeEnum.Directory.GetDescription()</span>';
                        }
                        else if (value == "@((int)MenuTypeEnum.Menu)") {
                            return '<span class="label label-primary">@MenuTypeEnum.Menu.GetDescription()</span>';
                        }
                        else if (value == "@((int)MenuTypeEnum.Button)") {
                            return '<span class="label label-warning">@MenuTypeEnum.Button.GetDescription()</span>';
                        }
                        else if (value == "@((int)MenuTypeEnum.NewPage)") {
                            return '<span class="label label-info">@MenuTypeEnum.NewPage.GetDescription()</span>';
                        }
                    }
                },
                { field: 'UrlAddress', title: '请求地址', align: "left", valign: 'middle' },
                { field: 'Icon', title: 'Icon图标', align: "left", valign: 'middle' },
                { field: 'Authorize', title: '权限标识', align: "center", valign: 'middle' },
                { field: 'DisplayOrder', title: '显示顺序', align: "center", valign: 'middle' },
            ],
            onLoadSuccess: function () {
                if (!!id) {
                    $('#gridTable').frameTreeTable('expandRowById', id);
                }
            }
        };
        $('#gridTable').frameTreeTable(options);
    }
    function searchTreeGrid(callBackId) {
        var param = $("#searchDiv").getWebControls();
        $('#gridTable').frameTreeTable('search', param);
        if (callBackId) {
            id = callBackId;
        }
    }
    function deleleNode() {
        var selectedRow = $("#gridTable").bootstrapTreeTable("getSelections");
        if (frame.checkRowDelete(selectedRow)) {
            frame.confirm("确定要删除该菜单吗？", function () {
                var id = frame.getIds(selectedRow);
                frame.ajax({
                    url: '@Url.Content("/Manage/Menu/DeleteData")' + '?id=' + id,
                    type: "post",
                    success: function (obj) {
                        if (obj.success) {
                            frame.msgSuccess(obj.message);
                            searchTreeGrid();
                        }
                        else {
                            frame.msgError(obj.message);
                        }
                    }
                });
            });
        }
    }
    function showForm(bAdd) {
        var id = "";
        var parentName = '';
        var selectedRow = $("#gridTable").bootstrapTreeTable("getSelections");
        if (!bAdd) {
            if (!frame.checkRowEdit(selectedRow)) {
                return;
            }
            id = selectedRow[0].Id;
        }
        frame.openDialog({
            title: !!id ? '编辑菜单' : '新增菜单',
            content: '@Url.Content("/Manage/Menu/Form")' + '?id=' + id,
            width: '580px',
            height: '480px',
            callback: function (index, layero) {
                var iframeWin = window[layero.find('iframe')[0]['name']];
                iframeWin.saveForm();
            }
        });
    }
</script> 